﻿using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace DevTools.PopupKiller.Components
{
    public class ProcessKiller
    {
        #region singleton
        private static ProcessKiller _instance;

        public static ProcessKiller Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ProcessKiller();
                return _instance;
            }
        }

        private ProcessKiller()
        {
            Data = new OptionDataSet();

            if (File.Exists(OptionFileName))
                Data.ReadXml(OptionFileName);
        }
        #endregion

        private const string OptionFileName = "Option.xml";

        public OptionDataSet Data { get; private set; }

        public OptionDataSet.TargetDataTable Target
        {
            get { return Data.Target; }
        }

        public static void Save()
        {
            foreach (var target in Instance.Target)
                target.Key = char.ToUpper(target.Key);

            Instance.Data.WriteXml(OptionFileName);
        }

        public void Kill(string processName)
        {
            var processes = Process.GetProcessesByName(processName);
            foreach (var processe in processes)
                processe.Kill();
        }
    }
}